<template>
  <div class="main">
    <div class="login_container" @keydown="keyenter">
      <div class="login_box">
        <div id="topTitle">
          <img src="../image/jshglogin.png" alt="" class="topImg" />
        </div>
        <el-form ref="loginFormRef" :rules="rules" class="login_form">
          <!-- 用户名 -->
          <el-form-item prop="userNo" label="社員番号:" class="form" label-width="90px">
            <el-input name="用户名" v-model="loginForm.userNo" class="inputpad"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="pwd" label="パスワード:" class="form" label-width="90px">
            <el-input v-model="loginForm.pwd" type="password" class="inputpad"></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="login" @keyup.enter="login">ログイン</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
export default {
  data() {
    return {
      loginForm: {
        userNo: '',
        pwd: ''
      },
      store: useStore(),
      meetingRoomOptions: []
    }
  },
  created() {
    // 按下enter键登录
    var _self = this
    window.onkeydown = function (e) {
      var key = window.event.keyCode
      if (_self.$route.path == '/login' && key == 13) {
        _self.login()
      }
    }

    var token = window.localStorage.getItem('token')
    if (token != null) {
      this.axios.post('/userInfo/login', {}).then((res) => {
        if (res.data.success) {
          this.$router.push('/home')
          this.store.commit('saveUserInfo', res.data.data.userInfo)
          this.store.commit('saveRouterInfo', res.data.data.routers)
        } else {
          window.localStorage.removeItem('token')
        }
      })
    }
  },
  methods: {
    login() {
      if (this.loginForm.userNo === '')
        return this.$message({
          showClose: true,
          message: this.$getMsg('W0003'),
          type: 'warning'
        })
      if (this.loginForm.pwd === '')
        return this.$message({
          showClose: true,
          message: this.$getMsg('W0004'),
          type: 'warning'
        })
      this.axios.post('/userInfo/login', this.loginForm).then((res) => {
        if (res.data.success) {
          // token
          window.localStorage.setItem('token', res.data.data.token)
          this.store.commit('saveTags', [
            {
              name: '/children',
              label: 'Top'
            }
          ])
          this.$router.push('/home')
          this.store.commit('saveUserInfo', res.data.data.userInfo)
          this.store.commit('saveRouterInfo', res.data.data.routers)
          // console.log(this.store.state.userInfo)
        } else {
          return this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.main {
  width: 100%;
  height: 100%;
  position: relative;
  background: url(../image/background.jpg) center top;
  background-size: cover;
}
.login_container {
  width: 608px;
  height: 358px;
  position: absolute;
  top: 50%;
  left: 50%;
  overflow: hidden;
  margin-top: -179px;
  margin-left: -304px;
  border-radius: 10px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
  z-index: 100;
  padding: 50px;
  box-sizing: border-box;
  border: 1px solid #a3a3a3;
}
.login_container::before {
  content: '';
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  z-index: -1;
  -webkit-filter: blur(20px);
  filter: blur(20px);
  filter: progid: DXImageTransform.Microsoft.Blur(PixelRadius=20, MakeShadow=false);
  -moz-filter: blur(20px);
  -ms-filter: blur(20px);
  margin: -30px;
  background: url(../image/background.jpg) center top;
  background-size: cover;
  background-attachment: fixed;
}
.login_box {
  width: 100%;
  height: 1005px;
}
#topTitle {
  width: 400px;
  height: 61.53px;
  margin: auto;
  margin-top: 30px;
  box-sizing: border-box;
}
.topImg {
  width: 100%;
  height: 100%;
}
.login_form {
  width: 100%;
  margin-top: 30px;
}
.form {
  width: 400px;
  margin-left: 40px;
}
.btns {
  text-align: center;
}
.inputpad > .el-input__inner {
  padding-left: 15px !important;
}
.login_form .el-form-item__label {
  margin-left: 38px !important;
}
</style>
